#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

class Solution
{
public:
    int findMaxLength(vector<int>& nums)
    {
        unordered_map<int, int> hash;
        hash[0] = -1;
        int sum = 0, len = 0;
        for(int i=0; i<nums.size(); i++)
        {
            if(nums[i]==0)
                sum += -1;
            else
                sum += 1;
            if(hash.count(sum))
                len = max(len, i-hash[sum]);
            else
                hash[sum] = i;
        }
        return len;
    }
};
